package com.edu.bhu.admin.controller.admin;

import java.awt.geom.Area;
import java.util.List;

import com.edu.bhu.admin.domain.PreArea;
import com.edu.bhu.admin.service.IPreAreaService;
import com.edu.bhu.admin.util.AreaBean;
import com.edu.bhu.common.annotation.Log;
import com.edu.bhu.common.core.controller.BaseController;
import com.edu.bhu.common.core.domain.AjaxResult;
import com.edu.bhu.common.core.page.TableDataInfo;
import com.edu.bhu.common.enums.BusinessType;
import com.edu.bhu.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 地区Controller
 *
 * @author yutianyang
 * @date 2022-04-06
 */
@Controller
@RequestMapping("/admin/area")
public class PreAreaController extends BaseController
{
    private String prefix = "admin/area";

    @Autowired
    private IPreAreaService preAreaService;

    @RequiresPermissions("admin:area:view")
    @GetMapping()
    public String area()
    {
        return prefix + "/area";
    }

    /**
     * 查询地区列表
     */
    @RequiresPermissions("admin:area:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PreArea preArea)
    {
        startPage();
        List<PreArea> list = preAreaService.selectPreAreaList(preArea);
        return getDataTable(list);
    }

    /**
     * 导出地区列表
     */
    @RequiresPermissions("admin:area:export")
    @Log(title = "地区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PreArea preArea)
    {
        List<PreArea> list = preAreaService.selectPreAreaList(preArea);
        ExcelUtil<PreArea> util = new ExcelUtil<PreArea>(PreArea.class);
        return util.exportExcel(list, "地区数据");
    }

    /**
     * 新增地区
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存地区
     */
    @RequiresPermissions("admin:area:add")
    @Log(title = "地区", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PreArea preArea)
    {
        return toAjax(preAreaService.insertPreArea(preArea));
    }

    /**
     * 修改地区
     */
    @RequiresPermissions("admin:area:edit")
    @GetMapping("/edit/{areaId}")
    public String edit(@PathVariable("areaId") Integer areaId, ModelMap mmap)
    {
        PreArea preArea = preAreaService.selectPreAreaByAreaId(areaId);
        mmap.put("preArea", preArea);
        return prefix + "/edit";
    }

    /**
     * 修改保存地区
     */
    @RequiresPermissions("admin:area:edit")
    @Log(title = "地区", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PreArea preArea)
    {
        return toAjax(preAreaService.updatePreArea(preArea));
    }

    /**
     * 删除地区
     */
    @RequiresPermissions("admin:area:remove")
    @Log(title = "地区", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(preAreaService.deletePreAreaByAreaIds(ids));
    }
}
